#include "vtor_timer.h"

#ifdef __VTOR_TIMER__

// 一直指向所有任务的头，不可更改
static VtorTimer* VtorTimer_arrayHeader = NULL;
static uint32_t VtorTimer_tick = 0;
static uint32_t VtorTimer_cnt = 0;

#ifdef __VTOR_TIMER_CONFIG_USAGE__
static uint32_t VtorTimer_usageInc = 0;
static uint32_t VtorTimer_usagePrecent = 0;
#endif

void VtorTimer_VoidFun()
{
	
}

void VtorTimer_Schedule(void)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if(NULL == timer)
	{
		return;
	}
	VtorTimer_cnt++;
	while (NULL != timer->timerFun)
	{
		if (VtorTimer_VoidFun != timer->timerFun)
		{
			if(VTOR_TIMER_STATE_READY == timer->state)
			{
				uint32_t endTime = 0;
				uint32_t startTime = VtorTimer_GetTick();
				timer->state = VTOR_TIMER_STATE_RUNNING;
				timer->timerFun();
				if(VTOR_TIMER_STATE_RUNNING == timer->state)
				{
					timer->state = VTOR_TIMER_STATE_WAITING;
				}
				timer->runCnt++;
				endTime = VtorTimer_GetTick();
				timer->runTime = endTime - startTime;
			}
		}
		timer++;
	}
}


void VtorTimer_IncTick(uint32_t tick)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if(NULL == timer)
	{
		return;
	}
	VtorTimer_tick += tick;
	
	// VtorTimer单位是毫秒，VtorTime单位是秒
	static int16_t tickCnt = 0;
	tickCnt += tick;
	while(tickCnt >= 1000)
	{
		tickCnt -= 1000;
		VtorTime_timeStamp += 1;
		VtorTime_SetTimeStamp(VtorTime_timeStamp);
	}
	while (NULL != timer->timerFun)
	{
		if (VtorTimer_VoidFun != timer->timerFun)
		{
			if(VTOR_TIMER_STATE_WAITING == timer->state)
			{
				timer->curTime += tick;
				if (timer->curTime >= timer->interval)
				{
					timer->curTime = 0;
					timer->state = VTOR_TIMER_STATE_READY;
				}
			}
		}
		timer++;
	}
#ifdef __VTOR_TIMER_CONFIG_CALLBACK__
	VtorTimer_IncTickCallback(tick);
#endif
}

uint32_t VtorTimer_GetTick(void)
{
	return VtorTimer_tick;
}

uint32_t VtorTimer_GetCnt(void)
{
	return VtorTimer_cnt;
}

void VtorTimer_Delay(uint32_t delayTick)
{
	uint32_t untilTick = VtorTimer_GetTick() + delayTick;

	while (VtorTimer_GetTick() < untilTick)
	{
	}
}

void VtorTimer_SetArrayHeader(VtorTimer* header)
{
	VtorTimer_arrayHeader = header;
}
VtorTimer* VtorTimer_GetArrayHeader()
{
	return VtorTimer_arrayHeader;
}
#ifdef __VTOR_TIMER_CONFIG_USAGE__
// 定时器使用率递增任务
void VtorTimer_TimerUsageIncTask(void)
{
	VtorTimer_usageInc++;
}
// 定时器使用率统计任务
void VtorTimer_TimerUsageCntTask(void)
{
	VtorTimer_usagePrecent = 101 - VtorTimer_usageInc;
	VtorTimer_usageInc = 0;
}
int32_t VtorTimer_GetTimerUsage(void)
{
	return VtorTimer_usagePrecent;
}
#endif // __VTOR_TIMER_CONFIG_USAGE__

VtorTimer* VtorTimer_GetRunningTimer()
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if (NULL == timer)
	{
		return NULL;
	}
	while (NULL != timer->timerFun)
	{
		if(VTOR_TIMER_STATE_RUNNING == timer->state)
		{
			return timer;
		}
		timer++;
	}
	return NULL;
}

VtorTimer* VtorTimer_GetTimerByFun(VtorTimerFun fun)
{
	VtorTimer* timer = VtorTimer_arrayHeader;
	if (NULL == timer || NULL == fun)
	{
		return NULL;
	}
	while(NULL != timer->timerFun)
	{
		if (fun == timer->timerFun)
		{
			return timer;
		}
		timer++;
	}
	return NULL;
}

VtorTimer* VtorTimer_Create(VtorTimerFun fun, int32_t interval)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL == timer)
	{
		timer = VtorTimer_GetTimerByFun(VtorTimer_VoidFun);
		if(NULL == timer)
		{
			VtorTimer_CreateFailCallback(fun);
			return NULL;
		}
		timer->timerFun = fun;
		VtorTimer_CreateCallback(timer->timerFun);
	}
	timer->curTime = 0;
	timer->interval = interval;
	timer->state = VTOR_TIMER_STATE_WAITING;
	return timer;
}


VtorTimer* VtorTimer_Start(VtorTimerFun fun, int32_t interval)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_WAITING;
		timer->interval = interval;
	}
	return timer;
}


VtorTimer* VtorTimer_Suspend(VtorTimerFun fun, int32_t tick)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		// 如果 是等待状态，那么已等待的时间减少一些
		if(VTOR_TIMER_STATE_WAITING == timer->state)
		{
			timer->curTime -= tick;
		}
		// 如果已就绪，回到等待状态，且设置等待时间为tick
		else if(VTOR_TIMER_STATE_READY == timer->state)
		{
			timer->state = VTOR_TIMER_STATE_WAITING;
			timer->curTime = timer->interval - tick;
		}
	}
	return timer;
}

void VtorTimer_RunNow(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);

	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_READY;
		timer->curTime = 0;
	}
}

void VtorTimer_Delete(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		if (VTOR_TIMER_STATE_STOP != timer->state)
		{
			VtorTimer_DeleteCallback(fun);
			timer->state = VTOR_TIMER_STATE_STOP;
			timer->timerFun = VtorTimer_VoidFun;
		}
		timer->runCnt = 0;
		timer->curTime = 0;
	}

}

void VtorTimer_Stop(VtorTimerFun fun)
{
	VtorTimer* timer = NULL;
	timer = VtorTimer_GetTimerByFun(fun);
	if(NULL != timer)
	{
		timer->state = VTOR_TIMER_STATE_STOP;
		timer->curTime = 0;
	}
}

// 以2025年4月1日，0点整作为默认时间吧
time_t VtorTime_timeStamp = 1743465600;
struct tm VtorTime_dateTime = {
	.tm_year = 2025 - 1900,		// 年份从 1900 开始计算
	.tm_mon = 4 - 1,			// 月份从 0 开始计算
	.tm_mday = 1,
	.tm_hour = 0,
	.tm_min = 0,
	.tm_sec = 0,
	.tm_isdst = -1,			// 自动判断夏令时;
};

int VtorTime_SetDateTime(int y, int m, int d, int hour, int min, int sec)
{
	VtorTime_dateTime.tm_year = y - 1900; // 年份从 1900 开始计算
	VtorTime_dateTime.tm_mon = m - 1;           // 月份从 0 开始计算
	VtorTime_dateTime.tm_mday = d;
	VtorTime_dateTime.tm_hour = hour;
	VtorTime_dateTime.tm_min = min;
	VtorTime_dateTime.tm_sec = sec;
	VtorTime_dateTime.tm_isdst = -1;         // 自动判断夏令时
	VtorTime_timeStamp = mktime(&VtorTime_dateTime); // 根据时间 获取时间戳
	return VtorTime_timeStamp;
}

int VtorTime_SetTimeStamp(time_t stamp)
{
	VtorTime_timeStamp = stamp;
	// 根据时间戳，获取带有日期和时间的结构体
	struct tm* tempDateInfo = localtime(&VtorTime_timeStamp);
	VtorTime_dateTime = *tempDateInfo;
	
//	char buf[80] = {0};
//	strftime(buf, sizeof (buf),
//		"%a %Y-%m-%d %H:%M:%S%Z", &VtorTime_dateTime);
//	VtorDebugPrintf ("%s \r\n", buf);
	
//	VtorDebugPrintf("%d-%d-%d %d:%d:%d\r\n",
//		VtorTime_dateTime.tm_year + 1900,
//		VtorTime_dateTime.tm_mon + 1,
//		VtorTime_dateTime.tm_mday,
//		VtorTime_dateTime.tm_hour,
//		VtorTime_dateTime.tm_min,
//		VtorTime_dateTime.tm_sec);
		
	return VtorTime_timeStamp;
}


#endif // __VTOR_TIMER__

